/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Domain;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainContentConstants;
import han.bifang.gwt.client.domain.DomainMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

public class DomainRootContent extends Content {
	private DomainMessages cons;
	private BifangGWTServiceAsync service;

	private VerticalPanel overviewPanel;
	private FlexTable local;
	private FlexTable remote;
	private Button nb = null;
	private GwtContext context;

	private AsyncCallback<ResponseObject> getDomainCallback = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			ArrayList ds = (ArrayList) res.getResponse();
			for (int i = 0; i < ds.size(); i++) {
				Domain d = (Domain) ds.get(i);
				if (d.getLocal()) {
					int row = local.getRowCount();
					local.setHTML(row, 0, d.getName());
					local.setHTML(row, 1, d.getDescription());

				} else {
					int row = remote.getRowCount();
					remote.setHTML(row, 0, d.getName());
					remote.setHTML(row, 1, d.getDescription());

				}
			}
			int localDomainCount = local.getRowCount() - 1;
			if (localDomainCount < 1)
				nb.setVisible(true);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public DomainRootContent(DomainMessages cons,
			BifangGWTServiceAsync service, GwtContext context) {
		this.cons = cons;
		this.service = service;
		this.context = context;

		overviewPanel = new VerticalPanel();
		overviewPanel.setSpacing(25);
		overviewPanel.setWidth("100%");

		VerticalPanel p1 = new VerticalPanel(), p2 = new VerticalPanel();
		p1.setSpacing(0);
		p2.setSpacing(0);
		p1.setWidth("100%");
		p2.setWidth("100%");

		HTML l = new HTML(cons.localDomainList());
		local = new FlexTable();
		local.setWidth("80%");

		local.setCellPadding(1);
		local.setCellSpacing(1);
		local.setBorderWidth(1);

		local.setHTML(0, 0, cons.name());
		local.setHTML(0, 1, cons.desc());

		ColumnFormatter cf = local.getColumnFormatter();
		cf.setWidth(0, "25%");
		cf.setWidth(1, "55%");

		RowFormatter rf = local.getRowFormatter();
		rf.setStyleName(0, "tableheader");

		nb = new Button(cons.newEntity());
		nb.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				HashMap t = new HashMap();
				t.put("local", new Boolean(true));

				listener.onContentChanged(DomainContentConstants.DOMAIN_ROOT,
						DomainContentConstants.CREATE_DOMAIN, t);

			}
		});
		nb.setVisible(false);

		HTML ll = new HTML(cons.remoteDomainList());
		remote = new FlexTable();
		remote.setWidth("80%");
		remote.setCellPadding(1);
		remote.setCellSpacing(1);
		remote.setBorderWidth(1);

		remote.setHTML(0, 0, cons.name());
		remote.setHTML(0, 1, cons.desc());

		cf = remote.getColumnFormatter();
		cf.setWidth(0, "25%");
		cf.setWidth(1, "55%");

		rf = remote.getRowFormatter();
		rf.setStyleName(0, "tableheader");

		Button nb1 = new Button(cons.newEntity());
		nb1.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				HashMap t = new HashMap();
				t.put("local", new Boolean(false));
				listener.onContentChanged(DomainContentConstants.DOMAIN_ROOT,
						DomainContentConstants.CREATE_DOMAIN, t);

			}
		});

		p1.add(l);
		p1.add(local);
		if (PrivilegChecker.hasPrivilege(context, null))
			p1.add(nb);

		p2.add(ll);
		p2.add(remote);
		if (PrivilegChecker.hasPrivilege(context, null))
			p2.add(nb1);

		overviewPanel.add(p1);
		overviewPanel.add(p2);

		initWidget(overviewPanel);

	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		req.setAction("han.bifang.gwt.server.action.domain.GetDomainAction");
		service.call(req, getDomainCallback);

	}

	public void onUnload() {
		local = null;
		remote = null;
	}

}
